package com.zmwx.wx.handler;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class WxChatMsgFactory implements InitializingBean {
    @Resource
    private List<WxChatMsgHandler> WxChatMsgHandlerList;
    private Map<WxChatMassageTypeEnum,WxChatMsgHandler> handlerMap=new HashMap<>();

    public WxChatMsgHandler getHandlerByMsgType(String msgType){

        WxChatMassageTypeEnum msgTypeEnum = WxChatMassageTypeEnum.getByMsgType(msgType);//根据消息类型获取消息类型枚举key
        return  handlerMap.get(msgTypeEnum);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (WxChatMsgHandler wxChatMsgHandler : WxChatMsgHandlerList){
            handlerMap.put(wxChatMsgHandler.getMsgType(),wxChatMsgHandler);
        }
        Set<WxChatMassageTypeEnum> keys=handlerMap.keySet();
        for (WxChatMassageTypeEnum mapkey:keys){
            System.out.println(mapkey.toString());
        }

    }
}
